package com.geek.homework;

import sun.misc.Launcher;

import java.io.*;
import java.lang.reflect.Method;
import java.net.MalformedURLException;
import java.net.URISyntaxException;
import java.net.URL;
import java.net.URLClassLoader;

/**
 * @author CJ
 * @date 2021年03月21日 23:03
 */
public class CjClassLoader extends ClassLoader{
    public static void main(String[] args) throws ClassNotFoundException, IllegalAccessException, InstantiationException {
       new CjClassLoader().findClass("Hello").newInstance();
    }

    @Override
    public Class<?> findClass(String name) throws ClassNotFoundException {
        URL resource = getResource("com/geek/homework/Hello.xlass");
        int count = 0;
        byte[] newBytes = null;
        try {
            File file = new File(resource.toURI());
            FileInputStream in = new FileInputStream(file);
            count = in.available();
            byte[]bytes = new byte[count];
            in.read(bytes);
            newBytes = convert(bytes);
            in.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return defineClass(name, newBytes, 0,newBytes.length);
    }

    private byte[] convert(byte[] bytes){
        for (int i = 0; i < bytes.length; i++) {
            bytes[i] = (byte)(255 - bytes[i]);
        }
        return bytes;
    }

}
